//
//  QiuAniVC.m
//  核心动画
//
//  Created by liuyanqi on 2018/5/18.
//  Copyright © 2018年 linch. All rights reserved.
//

#import "QiuAniVC.h"

@interface QiuAniVC ()
@property(nonatomic,strong)UIView *viewAni;
@property(nonatomic,strong)NSMutableArray *arrayQir;
@property(nonatomic,assign)NSInteger count;
@property(nonatomic,assign)NSInteger timeCount;
@property(nonatomic,assign)float timeDur;
@property(nonatomic,strong)NSTimer *timmer;
@end

@implementation QiuAniVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    _count = 10;
    _timeDur = 5.0;
    _timeCount = 0;
    [self setUpControlBtn];
    [self setUpAniView];
}
- (void)setUpControlBtn{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn];
    [btn setTitle:@"开始动画" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(beginAni) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(40, 40, 80, 40);
    btn.backgroundColor = [UIColor greenColor];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn1];
    [btn1 setTitle:@"增加" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(addQiu) forControlEvents:UIControlEventTouchUpInside];
    btn1.frame = CGRectMake(160, 40, 80, 40);
     btn1.backgroundColor = [UIColor greenColor];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn2];
    [btn2 setTitle:@"减少" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(jianshaoQiu) forControlEvents:UIControlEventTouchUpInside];
     btn2.frame = CGRectMake(160, 100, 80, 40);
     btn2.backgroundColor = [UIColor greenColor];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn3];
    [btn3 setTitle:@"同步" forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(neckAndNeck) forControlEvents:UIControlEventTouchUpInside];
    btn3.frame = CGRectMake(40, 100, 80, 40);
     btn3.backgroundColor = [UIColor greenColor];
    
    
    UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn4];
    [btn4 setTitle:@"增加速度" forState:UIControlStateNormal];
    [btn4 addTarget:self action:@selector(addSpeed) forControlEvents:UIControlEventTouchUpInside];
    btn4.frame = CGRectMake(280, 40, 80, 40);
    btn4.backgroundColor = [UIColor greenColor];
    
    UIButton *btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn5];
    [btn5 setTitle:@"减小速度" forState:UIControlStateNormal];
    [btn5 addTarget:self action:@selector(jianshaoSpeed) forControlEvents:UIControlEventTouchUpInside];
    btn5.frame = CGRectMake(280, 100, 80, 40);
    btn5.backgroundColor = [UIColor greenColor];
    
    
}
- (void)setUpAniView{
    
    
    self.viewAni = [[UIView alloc]init];
    [self.view addSubview:_viewAni];
    _viewAni.backgroundColor = [UIColor whiteColor];
    _viewAni.center = self.view.center;
    _viewAni.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width);
    
    CGPoint pointbig = [self.view convertPoint:self.view.center toView:_viewAni];
    
    UIView *big = [[UIView alloc]init];
    [_viewAni addSubview:big];
    big.center = pointbig;
    big.bounds = CGRectMake(0, 0, 300, 300);
    big.backgroundColor = [UIColor redColor];
    big.layer.cornerRadius = 150;
    big.clipsToBounds = YES;
    
    _arrayQir = [[NSMutableArray alloc]init];
    
    for (NSInteger i = 0; i < _count;i++) {
        UIView *viewQiu = [[UIView alloc]init];
        viewQiu.tag = i;
        viewQiu.bounds = CGRectMake(0, 0, 20, 20);
        [_viewAni addSubview:viewQiu];
        viewQiu.layer.cornerRadius = 10;
        viewQiu.clipsToBounds = YES;
        viewQiu.backgroundColor = [UIColor orangeColor];
        CGFloat xxx =pointbig.x +  (150 - 10) * cos(i * M_PI/_count);
        CGFloat yyy =pointbig.y +  (150 - 10) * sin(i * M_PI/_count);
        
        viewQiu.center = CGPointMake(xxx, yyy);
        [_arrayQir addObject:viewQiu];
    }
    
    
}


- (void)beginAni
{
    _timeCount = 0;
    _timmer = [NSTimer scheduledTimerWithTimeInterval:_timeDur/_count target:self selector:@selector(beginAnimation) userInfo:nil repeats:YES];
    [_timmer fire];
}
- (void)beginAnimation{
    CGPoint pointbig = [self.view convertPoint:self.view.center toView:_viewAni];
    
    if (_timeCount<_count) {
        
        UIView *viewQ = _arrayQir[_timeCount];
        [UIView beginAnimations:nil context:nil];
        //设置动画反向
        [UIView setAnimationRepeatAutoreverses:YES];
        //设置动画重复次数
        [UIView setAnimationRepeatCount:LONG_MAX];
        //设置移动速度
//        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        //设置动画时间
        [UIView setAnimationDuration: _timeDur];
        viewQ.center = CGPointMake(pointbig.x -  (150 - 10) * cos(_timeCount * M_PI/_count), pointbig.y -  (150 - 10) * sin(_timeCount * M_PI/_count));
        
        [UIView commitAnimations];
    };
    _timeCount ++;
    
}
//+个数
- (void)addQiu
{
    [_timmer invalidate];
    _timmer = nil;
    [_viewAni removeFromSuperview];
    _count ++;
    [self setUpAniView];
    [self beginAni];
    
    
}
//-个数
- (void)jianshaoQiu
{
    if (_count<2) {
        return;
    }
    [_timmer invalidate];
    _timmer = nil;
    [_viewAni removeFromSuperview];
    _count --;
    [self setUpAniView];
    [self beginAni];
}
//+速度
- (void)addSpeed
{
    if (_timeDur<0.2) {
        return;
    }
    [_timmer invalidate];
    _timmer = nil;
    [_viewAni removeFromSuperview];
    _timeDur = _timeDur - 0.1;
    [self setUpAniView];
    [self beginAni];
}
//-速度
- (void)jianshaoSpeed
{
    [_timmer invalidate];
    _timmer = nil;
    [_viewAni removeFromSuperview];
    _timeDur  = _timeDur+0.1;
    [self setUpAniView];
    [self beginAni];
}
//同步
- (void)neckAndNeck
{
    [_timmer invalidate];
    _timmer = nil;
    [_viewAni removeFromSuperview];
    
    
    self.viewAni = [[UIView alloc]init];
    [self.view addSubview:_viewAni];
    _viewAni.backgroundColor = [UIColor greenColor];
    _viewAni.center = self.view.center;
    _viewAni.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width);
    
    CGPoint pointbig = [self.view convertPoint:self.view.center toView:_viewAni];
    
    UIView *big = [[UIView alloc]init];
    [_viewAni addSubview:big];
    big.center = pointbig;
    big.bounds = CGRectMake(0, 0, 300, 300);
    big.backgroundColor = [UIColor orangeColor];
    big.layer.cornerRadius = 150;
    big.clipsToBounds = YES;
    
    
    CGPoint pointFuZhu = CGPointMake(pointbig.x-75, pointbig.y);
    
    _arrayQir = [[NSMutableArray alloc]init];
    for (NSInteger i = 0; i < _count;i++) {
        UIView *viewQiu = [[UIView alloc]init];
        viewQiu.tag = i;
        viewQiu.bounds = CGRectMake(0, 0, 20, 20);
        [_viewAni addSubview:viewQiu];
        viewQiu.layer.cornerRadius = 10;
        viewQiu.clipsToBounds = YES;
        viewQiu.backgroundColor = [UIColor purpleColor];
        CGFloat xxx =pointFuZhu.x +  (75 - 10) * cos(i * 2*M_PI/_count);
        CGFloat yyy =pointFuZhu.y +  (75 - 10) * sin(i * 2*M_PI/_count);
        
        viewQiu.center = CGPointMake(xxx, yyy);
        [_arrayQir addObject:viewQiu];
    }
    
    [NSThread sleepForTimeInterval:2];
    
    for (UIView *viewq in _arrayQir) {
        
        [UIView beginAnimations:nil context:nil];
        //设置动画反向
        [UIView setAnimationRepeatAutoreverses:YES];
        //设置动画重复次数
        [UIView setAnimationRepeatCount:LONG_MAX];
        //设置移动速度
        //        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        //设置动画时间
        [UIView setAnimationDuration: _timeDur];
        viewq.center = CGPointMake(pointFuZhu.x -  (75 - 10) * cos(viewq.tag * 2 *M_PI/_count), pointFuZhu.y -  (75 - 10) * sin(viewq.tag * 2 *M_PI/_count));
        
        [UIView commitAnimations];
    };
    
    
}

@end
